package main;

import client.RPCClient;
import java.io.File;
import java.util.HashMap;
import utils.ExceptionUtils;

/**
 * Allows a user to run an XML RPC client and retrieve results from calling a method on the service.
 * @date Mar 26, 2011
 * @author Eric Kisner
 */
public class Main {

    private static final String HANDLER = "shape_math";
    private static final String XML_INPUT = "xml/MethodInput_ShapeMath.xml";
    private static final int PORT = 8888;
    private static final String HOST = "localhost";

    public static void main( String[] args ) {
        RPCClient client;
        //If the user has passed in arguments, they are specifying the port, host address, and XML config file.
        if( args.length > 0 ) {
            String host = null;
            String filePath = null;
            int port = -1;

            //Get port number from first argument
            try {
                port = Integer.parseInt( args[0] );
            } catch( NumberFormatException nfe ) {
                invalidPort( args[0] );
            }

            //Validate host address from second argument
            if( args[1] == null ) {
                usage();
            } else {
                host = args[1];
            }

            //Validate XML config file from third argument
            if( args[2] == null ) {
                usage();
            } else {
                filePath = args[2];
                if( !(new File( filePath ).exists()) || !filePath.contains( ".xml" ) ) {
                    invalidFilePath( filePath );
                }
            }

            client = new RPCClient( port, host, HANDLER, filePath );

        } else {    //The user wants default port, address, and config file
            client = new RPCClient( PORT, HOST, HANDLER, XML_INPUT );
        }

        try {
            HashMap<String, String> results = client.getResults();
            for( String methodName : results.keySet() ) {
                System.out.println( "Method '" + methodName + "' returned '" + results.get( methodName ) + "'." );
            }
        } catch( Exception e ) {
            System.err.println( "An error occurred when trying to get results from the XML RPC service." );
            ExceptionUtils.handleException( e );
            System.exit( -1 );
        }
    }

    private static void usage() {
        System.err.println( "Usage to specify port, host address, and XML config file path: "
                            + "Main [port#] [host address] [XML config path]" );
        System.err.println( "To use defaults of 'localhost:8888' and 'xml/MethodInput_ShapeMath.xml', "
                            + "do not input port or address." );
        System.exit( 0 );
    }

    private static void invalidPort( String port ) {
        System.err.println( "Specified port '" + port + "' must be a number." );
        System.exit( 0 );
    }

    private static void invalidFilePath( String filePath ) {
        System.err.println( "Specified XML config file '" + filePath + "' is an invalid file." );
        System.exit( 0 );
    }
}
